﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using LitJson;

namespace ReadValue
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "剧本文件|*.json";
            dialog.Multiselect = true;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (string path in dialog.FileNames)
                {
                    LoadList.Text += path + ";";
                }
            }
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            string[] paths = LoadList.Text.Split(new char[] { ';' });
            List<string> result = new List<string>();
            foreach(string path in paths)
            {
                if (File.Exists(path))
                {
                    JsonData json = JsonMapper.ToObject(File.ReadAllText(path));
                    foreach(string key in json.Keys)
                    {
                        JsonData data = json[key];
                        if (data["CommandType"].ToString() == CommandName.Text && data[CommandValue.Text].ToString() != "")
                        {
                            string text = data[CommandValue.Text].ToString();
                            if (!result.Contains(text))
                            {
                                result.Add(text);
                            }
                        }
                    }
                }
            }
            ResultList.Text = "";
            foreach(string text in result)
            {
                ResultList.Text += text + "\n";
            }
        }
    }
}
